﻿/*
 * Test classes for SSAS Partition Manager 
 * https://ssaspartitionmanager.codeplex.com/
 * 
 * Written by Dr. John Tunnicliffe of Decision Analytics, May 2015
 * eMail: john@decision-analytics.co.uk 
 * http://www.decision-analytics.co.uk/
 * https://www.linkedin.com/in/drjohntunnicliffe
 * 
 * Tests written with FluentAssertions, XUnit and Moq which can be installed from Nuget.  
 * 
 * FluentAssertions
 * http://www.fluentassertions.com/ 
 * 
 * XUnit
 * http://xunit.github.io/
 * 
 * Moq
 * https://github.com/Moq/moq4
 */

namespace AmoExtensions.IntegrationTests
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Dynamic;

    using AmoExtensions;
    using FluentAssertions;
    using Xunit;
    using Moq;

    public class PartitionManagerTabularFixture : IDisposable
    {
        public readonly PartitionManager partitionManager;
        public readonly string TabularDatabaseName;
        public const string TabularModelName = "Model";
        public readonly IAmoServer TabularServer;

        /// <summary>
        /// As per the xUnit.net mantra, test setup and teardown is done in the constructor and Dispose() methods respectively
        /// </summary>
        public PartitionManagerTabularFixture()
        {
            TabularServer = new AmoServer();
            if (FixtureHelperFunctions.ConnectTabularServer(TabularServer))
            {
                // generate a name and deploy the cube for testing
                TabularDatabaseName = FixtureHelperFunctions.GetUniqueSsasDatabaseName();

                if (FixtureHelperFunctions.DeployTestModel(TabularServer, TabularDatabaseName))
                {
                    // setup fixture data here

                    // ok let's start by getting the normal list of partitions to deploy and then edit the entries
                    // we are restricting the list to Internet Sales which is 16 quarterly partitions
                    string sql = @"
select
	SsasDatabaseName,
	SsasModelType,
	SsasCubeName,
	SsasMeasureGroupName,
	PartitionName,
    PartitionWhereClause,
    PartitionSlice
from spm.SsasPartitions 
where SsasMeasureGroupName = N'Internet Sales' and SsasModelType=N'Tabular'
";

                    IEnumerable<Partition> ListOfPartitions = FixtureHelperFunctions.QueryDynamic(sql)
                        .Select(x => Partition.FromDynamic(TabularServer, x))
                        .Cast<Partition>()  // cast the dynamic object into a partition object
                        .ToList(); // convert everything to a list object

                    // correct the database name
                    foreach (Partition partition in ListOfPartitions)
                    {
                        partition.DatabaseName = TabularDatabaseName;
                        // we now setup specific test data by changing specific entries

                        // the following four partitions will not be valid
                        if (partition.PartitionName == "Internet Sales 2011M01")
                        {
                            partition.DatabaseName = FixtureHelperFunctions.GetUniqueSsasDatabaseName();
                        }

                        if (partition.PartitionName == "Internet Sales 2011M02")
                        {
                            partition.CubeName = FixtureHelperFunctions.GetUniqueSsasDatabaseName();
                        }

                        if (partition.PartitionName == "Internet Sales 2011M03")
                        {
                            partition.MeasureGroupName = FixtureHelperFunctions.GetUniqueSsasDatabaseName();
                        }

                        if (partition.PartitionName == "Internet Sales 2011M04")
                        {
                            partition.PartitionWhereClause = null;
                        }

                        if (partition.PartitionName == "Internet Sales 2011M05")
                        {
                            // this does not have a template partition, so should be invalid
                            partition.MeasureGroupName = "Sales Quota";
                        }
                    }

                    partitionManager = new PartitionManager(TabularServer, ListOfPartitions);
                }
            }
        }

        public void Dispose()
        {
            // drop the test Tabular database  
            TabularServer.DropDatabase(TabularDatabaseName);

            // disconnect from the server
            TabularServer.Dispose();
        }
    }
}
